<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Captcha extends CI_Controller
{
	function create_captcha()
	{
	$this->load->helper('string');
	$options = array(
	'img_path' => './asset/admin/captcha/',
	'img_url' => base_url().'asset/admin/captcha/',
	'img_width' => '50',
	'img_height' => '30',
	'expiration' => 7200,
	'word' => random_string('numeric',4),
	);
	$cap = create_captcha($options);
	$image = $cap['image'];
	$this->session->set_userdata('captchaword',$cap['word']);
	return $image;
	}
	
	function check_captcha()
	{
		if ($this->input->post('captcha') == $this->session->userdata('captchaword'))
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('Periksa Captcha','Captcha salah');
			return false;
		}
	}	
	function index()
	{
		$this -> form_validation->set_rules('captcha','Captcha','trim|callback_check_captcha|required');
		if ($this->form_validation->run() == false)
		{
			$this->load->view('captcha/index',array('img'=>$this->create_captcha()));
		}
		else
		{
			echo 'Kode Captcha Sukses';
		}
	}
	}